<?php
/**
 * 封装 DsCache, 单件模式 更易操作;
 * @filesource Cache.class.php
 * @package DsCache
 * @subpackage 
 * @version $id: 2.0, utf8, Sat Dec  5 14:10:38 CST 2009
 * @author LD King <kldscs[at]gmail.com>
 * @copyright Copyleft (D.) 2007 - 2009 MiFunny China Inc.
 * @link http://mifunny.info/
 * @example
 *   require_once 'Cache.class.php';
 *   
 *   //文件缓存
 *   $_config['DsCache'] = array(
 *   	'cache_dir' => '/tmp', //缓存目录(必须)
 *   	'life_time' => 900, //全局缓存时间 900s.
 *   	);
 *   
 *   //内存缓存
 *   $_config['DsCache'] = array(
 *   	'cache_type' => 'phpmencached' //缓存类型(必须)
 *   	'servers' => array(		//缓存服务器,至少填一个(必需)
 *   			array( 'host' => '127.0.0.1', 'port' => '11211', 'weight' => 99, ),
 *   			array( 'host' => '192.168.1.112', 'port' => '11211', 'weight' => 32, ),	
 *   			), 
 *   	'life_time' => 900, //全局缓存时间 900s.
 *   	'persistent' => true, //持久连接
 *   	'compressed' => false, //压缩数据与否
 *   	'encoding_key' => false, //加密缓存ID, 即 MD5($key)
 *   	);
 *   
 *   //约定: 如果 'life_time' 设置为 0 表示不检查缓存有效期(即缓存永久有效).
 *
 *   Cache::set('select * from test', $data); //缓存变量
 *   Cache::get('cache id 1234'); //得到缓存
 *   Cache::remove('my cache file'); //清除特定缓存文件
 *   Cache::clean(); //清空缓存目录
 */
require_once 'spl_autoload.func.php';
/**
 * 单件缓存, walala
 */
class Cache{
    private static $_instance;
    public $cache;

    /**
     * 构造,析构函数
     */
    private function __construct(){}
    private function __clone(){}

    /**
     * 获得缓存静态对像
     */
    public static function getInstance(array $default_policy = null){
        if( !(self::$_instance instanceof self) ){
			if( is_null($default_policy) ) {
				global $_config;
				$policy = $_config['DsCache'];
			}else{
				$policy = $default_policy;
			}
            
            if ( empty($policy) ) {
        		throw new RuntimeException("Error: Someone should give any stuff to \$_config['DsCache'] !");
        	}
			
        	$cache_type = $policy['cache_type'] ? $policy['cache_type'] : 'datafile';
            self::$_instance = new self();
            
            if( interface_exists('DsCache') && class_exists($cache_type) ){
            	self::$_instance->cache = self::_init(new $cache_type, $policy);
            }else throw new LogicException("Error: Undefined cache type('{$cache_type}') !");
        }
        return self::$_instance;
    }

    /**
     * 初始化 缓存-
     *
     * @param DsCache $c 缓存对像
     * @param array $policy 初始策略
     *
     * @return DsCache $cache
     */
    private static function _init(DsCache $c, array $policy=null){
        $c->_init( $policy ); 
        return $c;
    }
        
    public static function set($id, $data, $expiration=null){
        return self::getInstance()->cache
            ->set($id, $data, $expiration);
    }

    public static function get($id, $expiration=null){
        return self::getInstance()->cache
            ->get($id, $expiration);
    }

    public static function remove($id, $delay=0){
        return self::getInstance()->cache->remove($id, $delay);
    }

    public static function clean($delay=0){
        return self::getInstance()->cache->clean($delay);
    }

}//END class Cache
?>
